package com.sztx.pay.center.core.service.impl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.sun.tools.javac.code.Attribute.Array;
import com.sztx.pay.center.common.util.ConfigUtil;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.CurrencyUtil;
import com.sztx.pay.center.common.util.DateUtil;
import com.sztx.pay.center.common.util.Constance.PayFlowType;
import com.sztx.pay.center.common.util.Constance.TradeOrderType;
import com.sztx.pay.center.common.yhbf.YHBFSensitiveEnc;
import com.sztx.pay.center.common.yhbf.YHBFUtil;
import com.sztx.pay.center.core.service.DumpDataHandleService;
import com.sztx.pay.center.core.service.DumpQueryService;
import com.sztx.pay.center.core.service.RiskTradeTypeService;
import com.sztx.pay.center.core.service.bean.rpc.OrderB2C;
import com.sztx.pay.center.core.service.bean.rpc.OrderCallback;
import com.sztx.pay.center.core.service.bean.rpc.OrderCallbackList;
import com.sztx.pay.center.core.service.bean.rpc.OrderRecharge;
import com.sztx.pay.center.core.service.bean.rpc.OrderRefund;
import com.sztx.pay.center.core.service.bean.rpc.OrderSettle;
import com.sztx.pay.center.core.service.bean.rpc.OrderSettleList;
import com.sztx.pay.center.core.service.bean.rpc.OrderTransfer;
import com.sztx.pay.center.core.service.bean.rpc.OrderWithdraw;
import com.sztx.pay.center.core.service.bean.rpc.PayFlow;
import com.sztx.pay.center.core.service.bean.rpc.PayFlowList;
import com.sztx.pay.center.core.service.bean.rpc.QueryOrderCallback;
import com.sztx.pay.center.core.service.bean.rpc.QueryOrderRequest;
import com.sztx.pay.center.core.service.bean.rpc.QueryOrderSettle;
import com.sztx.pay.center.core.service.bean.rpc.QueryUserAccountFlow;
import com.sztx.pay.center.core.service.bean.rpc.UserActFlow;
import com.sztx.pay.center.core.service.bean.rpc.UserActFlowList;
import com.sztx.pay.center.dataaccess.dao.hbase.DumpOrderHbaseDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.DumpRecordMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderB2cMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderRechargeMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderRefundMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderTransferMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderWithdrawMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskTradeTypeMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.CallbackNotifyDO;
import com.sztx.pay.center.dataaccess.domain.DumpOrderDetailHbaseDO;
import com.sztx.pay.center.dataaccess.domain.DumpOrderHbaseDO;
import com.sztx.pay.center.dataaccess.domain.DumpUserActFlowHbaseDO;
import com.sztx.pay.center.dataaccess.domain.OrderB2cDumpDO;
import com.sztx.pay.center.dataaccess.domain.OrderRechargeDumpDO;
import com.sztx.pay.center.dataaccess.domain.OrderRefundDO;
import com.sztx.pay.center.dataaccess.domain.OrderRefundDumpDO;
import com.sztx.pay.center.dataaccess.domain.OrderTransferDO;
import com.sztx.pay.center.dataaccess.domain.OrderTransferDumpDO;
import com.sztx.pay.center.dataaccess.domain.OrderWithdrawDumpDO;
import com.sztx.pay.center.dataaccess.domain.PayFlowDO;
import com.sztx.pay.center.dataaccess.domain.RiskTradeTypeDO;
import com.sztx.pay.center.dataaccess.domain.SettleOrderDO;
import com.sztx.pay.center.dataaccess.domain.UserActFlowDO;
import com.sztx.pay.center.dataaccess.domain.UserActFlowDumpDO;
import com.sztx.pay.center.dataaccess.entity.QueryOrder;
import com.sztx.se.common.util.StringUtil;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.rpc.dubbo.client.DubboClientFactory;
import com.sztx.trade.payment.gateway.rpc.api.domain.routing.ChannelRsp;
import com.sztx.trade.payment.gateway.rpc.api.service.routing.ChannelRpcService;

@Service
public class DumpDataHandleServiceImpl extends BaseServiceImpl implements DumpDataHandleService {

	@Autowired
	private OrderB2cMysqlDAO orderB2cMysqlDAO;

	@Autowired
	private OrderRechargeMysqlDAO orderRechargeMysqlDAO;

	@Autowired
	private OrderRefundMysqlDAO orderRefundMysqlDAO;
	
	@Autowired
	private DumpRecordMysqlDAO dumpRecordMysqlDAO;

	@Autowired
	private DumpOrderHbaseDAO dumpOrderHbaseDAO;
	
	@Autowired
	private OrderWithdrawMysqlDAO orderWithdrawMysqlDAO;
	
	@Autowired
	private OrderTransferMysqlDAO orderTransferMysqlDAO;
	
	@Autowired
	private UserActFlowMysqlDAO userActFlowMysqlDAO;

	@Autowired
	private DumpQueryService dumpQueryService;
	
	@Autowired
	RiskTradeTypeMysqlDAO riskTradeTypeMysqlDAO;
	
	private static final String DEFAULTTYPE = "未知";
	
	private static final String DEFAULWITHTTYPE = "提现到余额";
	
	private Map<String, String> payChannelList = null;
	
	@Autowired
    private DubboClientFactory dubboClientFactory;

	@Autowired
	RiskTradeTypeService riskTradeTypeService;
	
	@Override
	public List<OrderB2cDumpDO> findUnHandleB2cOrder(Integer taskItemNum, List<String> taskItems, Integer fetchNum) {

		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		int dumpStartDayByPlus = ConfigUtil.dumpStartDayByPlus;
		int dumpOrderStartDay = beforeDay + dumpStartDayByPlus;
		Date dumpOrderStartDate = getBeforeDate(new Date(), dumpOrderStartDay);
		String startTime = null;
		
		if (dumpStartDayByPlus >= 0) {
			startTime = DateUtil.formatDate(dumpOrderStartDate, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
		}
		
		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";

		return orderB2cMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum, startTime, endTime);
	}

	@Override
	public boolean updateHandleB2cOrder(String tradeOrderNo, List<Integer> oldStatus, Integer newStatus, Integer num) {
		orderB2cMysqlDAO.updateHandleForSchedule(tradeOrderNo, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public boolean batchUpdateHandleB2cOrder(OrderB2cDumpDO[] orderB2cDOs, List<Integer> oldStatus, Integer newStatus, Integer num) {
		List<String> orderNos = new ArrayList<String>();
		for (OrderB2cDumpDO orderB2cDO : orderB2cDOs) {
			orderNos.add(orderB2cDO.getTradeOrderId());
		}
		orderB2cMysqlDAO.batchUpdateHandleForSchedule(orderNos, oldStatus, newStatus, num);
		return false;
	}

	/*
	 * 迁移order_b2c表的数据
	 */
	@Override
	public void dumpB2cOrder(OrderB2cDumpDO[] orderB2cDOs) {

		List<DumpOrderHbaseDO> hbaseDOs = new ArrayList<DumpOrderHbaseDO>();
		List<DumpOrderDetailHbaseDO> orderDetails = new ArrayList<DumpOrderDetailHbaseDO>();
		
		for (OrderB2cDumpDO orderB2cDO : orderB2cDOs) {
			DumpOrderHbaseDO hbaseDO = renderHbaseDO(orderB2cDO.getBusinessType(), orderB2cDO.getOrderStatus(), orderB2cDO.getPayeeUserId(), orderB2cDO.getPayerUserId(), orderB2cDO.getOrderId(),
					orderB2cDO.getTradeOrderId(), Constance.DumpRecordType.B2C.getCode(), orderB2cDO.getAddTime(), orderB2cDO.getPlatChannelId());
			
			OrderB2C data = renderOrderB2C(orderB2cDO);
			String jsonData = JSON.toJSONString(data, new NumberValueFilter());
			hbaseDO.setData(jsonData);
			//构造订单详情
			String tradeOrderId = orderB2cDO.getTradeOrderId();
			DumpOrderDetailHbaseDO detailHbaseDO = new DumpOrderDetailHbaseDO();
			detailHbaseDO.setTradeOrderId(tradeOrderId);
			detailHbaseDO.setActFlowData(JSON.toJSONString(renderUserActFlowDOList(orderB2cDO.getUserActFlows()), new NumberValueFilter()));
			detailHbaseDO.setCallbackData(JSON.toJSONString(renderCallbackNotifyDOList(orderB2cDO.getOrderCallbacks()), new NumberValueFilter()));
			detailHbaseDO.setDetailData(jsonData);
			detailHbaseDO.setSettleData(JSON.toJSONString(renderSettleOrderDOList(orderB2cDO.getSettleOrders()), new NumberValueFilter()));
			detailHbaseDO.setPayFlowData(JSON.toJSONString(renderPayFlowDOList(orderB2cDO.getPayFlows()), new NumberValueFilter()));
			detailHbaseDO.setRefundData(JSON.toJSONString(renderRefundDOList(orderB2cDO.getRefundOrders()), new NumberValueFilter()));
			orderDetails.add(detailHbaseDO);
			hbaseDOs.add(hbaseDO);
		}
		
		dumpOrderHbaseDAO.batchAddDumpOrder(hbaseDOs);
		dumpOrderHbaseDAO.batchAddOrderDetail(orderDetails);
	}
	
	@Override
	public List<UserActFlowDumpDO> findUnHandleUserActFlow(Integer taskItemNum, List<String> taskItems, Integer fetchNum) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		int dumpStartDayByPlus = ConfigUtil.dumpStartDayByPlus;
		int dumpOrderStartDay = beforeDay + dumpStartDayByPlus;
		Date dumpOrderStartDate = getBeforeDate(new Date(), dumpOrderStartDay);
		String startTime = null;
		
		if (dumpStartDayByPlus >= 0) {
			startTime = DateUtil.formatDate(dumpOrderStartDate, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
		}
		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";

		return userActFlowMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum, startTime, endTime);
	}

	@Override
	public boolean updateHandleUserActFlow(String tradeOrderNo, List<Integer> oldStatus, Integer newStatus, Integer num) {
		userActFlowMysqlDAO.updateHandleForSchedule(tradeOrderNo, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public boolean batchUpdateHandleUserActFlow(UserActFlowDumpDO[] userActFlowDumpDODOs, List<Integer> oldStatus, Integer newStatus, Integer num) {
		List<String> orderNos = new ArrayList<String>();
		for (UserActFlowDumpDO userActFlowDumpDO : userActFlowDumpDODOs) {
			orderNos.add(userActFlowDumpDO.getActFlowNo());
		}
		userActFlowMysqlDAO.batchUpdateHandleForSchedule(orderNos, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public void dumpUserActFlow(UserActFlowDumpDO[] userActFlowDumpDOs) {
		List<DumpUserActFlowHbaseDO> hbaseDOs = new ArrayList<DumpUserActFlowHbaseDO>();
		for (UserActFlowDumpDO userActFlowDumpDO : userActFlowDumpDOs) {
			DumpUserActFlowHbaseDO hbaseDO = new DumpUserActFlowHbaseDO();
			
			String payeeUserId = userActFlowDumpDO.getPayeeUserId();
			Integer payeeActType = userActFlowDumpDO.getPayeeActType();
			String payerUserId = userActFlowDumpDO.getPayerUserId();
			Integer payerActType = userActFlowDumpDO.getPayerActType();
			String accountId = userActFlowDumpDO.getUserId();
			Integer tradeType = userActFlowDumpDO.getTradeType();
			Integer payFlowType = userActFlowDumpDO.getPayFlowType();
			long tradeAmount = userActFlowDumpDO.getTranAmount();
			String payFlowInnerRemark = userActFlowDumpDO.getPayFlowInnerRemark();
			userActFlowDumpDO.setTradeTypeDesc(TradeOrderType.valueOf(userActFlowDumpDO.getTradeType()).getDesc());
			userActFlowDumpDO.setBusinessDesc(getBusinessTypeDesc(userActFlowDumpDO.getBusinessType()));
			if (StringUtils.isEmpty(payerUserId) && StringUtils.isEmpty(payeeUserId)) {
				continue;
			}
			if (TradeOrderType.B2C_TRADE_ORDER.getCode() == tradeType) {
				if (PayFlowType.PAY.getCode() == payFlowType) {
					userActFlowDumpDO.setPayerUserId(Constance.USER_SZTX);
					userActFlowDumpDO.setPayerActType(Constance.UserActType.CASH.getCode());
					userActFlowDumpDO.setPayeeUserId(payeeUserId);
					userActFlowDumpDO.setPayeeActType(payeeActType);
				} else {
					userActFlowDumpDO.setPayerUserId(payerUserId);
					userActFlowDumpDO.setPayerActType(payerActType);
					userActFlowDumpDO.setPayeeUserId(payeeUserId);
					userActFlowDumpDO.setPayeeActType(payeeActType);
				}
			}
			
			if (TradeOrderType.CHARGE_ORDER.getCode() == tradeType  ) {
				userActFlowDumpDO.setPayerUserId(Constance.USER_SZTX);
				userActFlowDumpDO.setPayerActType(Constance.UserActType.CASH.getCode());
				userActFlowDumpDO.setPayeeUserId(payeeUserId);
				userActFlowDumpDO.setPayeeActType(payeeActType);
			}
			
			if (TradeOrderType.SETTLE_ORDER.getCode() == tradeType || TradeOrderType.SETTLE_PROFIT.getCode() == tradeType || TradeOrderType.SETTLE_DISTRIBUTOR.getCode() == tradeType || TradeOrderType.TRANSFER_ORDER.getCode() == tradeType || TradeOrderType.BIND_CARD_ORDER.getCode() == tradeType || TradeOrderType.BALANCE_TRANSFER.getCode() == tradeType) {
				userActFlowDumpDO.setPayerUserId(payerUserId);
				userActFlowDumpDO.setPayerActType(payerActType);
				userActFlowDumpDO.setPayeeUserId(payeeUserId);
				userActFlowDumpDO.setPayeeActType(payeeActType);
			}
			
			if (TradeOrderType.BALANCE_WITHDRAW_ORDER.getCode() == tradeType || TradeOrderType.PROFIT_WITHDRAW_ORDER.getCode() == tradeType ) {
				userActFlowDumpDO.setPayerUserId(payerUserId);
				userActFlowDumpDO.setPayerActType(payerActType);
				userActFlowDumpDO.setPayeeUserId(Constance.USER_SZTX);
				userActFlowDumpDO.setPayeeActType(Constance.UserActType.CASH.getCode());
			}
			
			if (TradeOrderType.REFUND_ORDER.getCode() == tradeType) {
				userActFlowDumpDO.setPayerUserId(payerUserId);
				userActFlowDumpDO.setPayerActType(payerActType);
				if (StringUtils.isNotBlank(payFlowInnerRemark) && payFlowInnerRemark.startsWith("3")) {
					userActFlowDumpDO.setPayeeUserId(Constance.USER_SZTX);
					userActFlowDumpDO.setPayeeActType(Constance.UserActType.CASH.getCode());
				} else {
					userActFlowDumpDO.setPayeeUserId(payeeUserId);
					userActFlowDumpDO.setPayeeActType(payeeActType);
				}
			}
			
			if (TradeOrderType.TRADE_FEE.getCode() == tradeType) {
				userActFlowDumpDO.setPayerUserId(payerUserId);
				if  (payerActType == null) {
					userActFlowDumpDO.setPayerActType(Constance.UserActType.CASH.getCode());
				} else {
					userActFlowDumpDO.setPayerActType(payerActType);
				}
				userActFlowDumpDO.setPayeeUserId(Constance.USER_PROFIT);
				userActFlowDumpDO.setPayeeActType(Constance.UserActType.CASH.getCode());
			}
			
			hbaseDO.setActFlowNo(userActFlowDumpDO.getActFlowNo());
			hbaseDO.setActType(userActFlowDumpDO.getActType());
			hbaseDO.setBussinessType(userActFlowDumpDO.getBusinessType());
			hbaseDO.setCreateDate(userActFlowDumpDO.getCreateTime());
			hbaseDO.setOriginOrderNo(userActFlowDumpDO.getOriginOrderNo());
			hbaseDO.setTradeAmount(tradeAmount);
			hbaseDO.setTradeType(userActFlowDumpDO.getTradeType());
			hbaseDO.setUserId(userActFlowDumpDO.getUserId());
			hbaseDO.setModifyCount(userActFlowDumpDO.getModifyCount());
			
			String jsonData = JSON.toJSONString(userActFlowDumpDO, new NumberValueFilter());
			hbaseDO.setData(jsonData);
			hbaseDOs.add(hbaseDO);
		}
		
		
		dumpOrderHbaseDAO.addDumpUserActFlow(hbaseDOs);
	}
	
	@Override
	public List<OrderTransferDumpDO> findUnHandleTransferOrder(Integer taskItemNum, List<String> taskItems, Integer fetchNum) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		int dumpStartDayByPlus = ConfigUtil.dumpStartDayByPlus;
		int dumpOrderStartDay = beforeDay + dumpStartDayByPlus;
		Date dumpOrderStartDate = getBeforeDate(new Date(), dumpOrderStartDay);
		String startTime = null;
		
		if (dumpStartDayByPlus >= 0) {
			startTime = DateUtil.formatDate(dumpOrderStartDate, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
		}
		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";

		return orderTransferMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum, startTime, endTime);
	}

	@Override
	public boolean updateHandleTransferOrder(String tradeOrderNo, List<Integer> oldStatus, Integer newStatus, Integer num) {
		orderTransferMysqlDAO.updateHandleForSchedule(tradeOrderNo, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public boolean batchUpdateHandleTransferOrder(OrderTransferDumpDO[] orderTransferDOs, List<Integer> oldStatus, Integer newStatus, Integer num) {
		List<String> orderNos = new ArrayList<String>();
		for (OrderTransferDumpDO orderTransferDO : orderTransferDOs) {
			orderNos.add(orderTransferDO.getTransferOrderId());
		}
		orderTransferMysqlDAO.batchUpdateHandleForSchedule(orderNos, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public void dumpTransferOrder(OrderTransferDumpDO[] orderTransferDOs) {

		List<DumpOrderHbaseDO> hbaseDOs = new ArrayList<DumpOrderHbaseDO>();
		List<DumpOrderDetailHbaseDO> orderDetails = new ArrayList<DumpOrderDetailHbaseDO>();
		
		for (OrderTransferDumpDO orderTransferDO : orderTransferDOs) {
			
			DumpOrderHbaseDO hbaseDO = renderHbaseDO(orderTransferDO.getBusinessType(), (int)orderTransferDO.getOrderStatus(), orderTransferDO.getPayeeUserId(), orderTransferDO.getPayerUserId(), orderTransferDO.getOrderId(),
					orderTransferDO.getTransferOrderId(), Constance.DumpRecordType.TRANSFER.getCode(), orderTransferDO.getAddTime(), orderTransferDO.getPlatChannelId());
			
			OrderTransfer data = renderOrderTransfer(orderTransferDO);
			String jsonData = JSON.toJSONString(data, new NumberValueFilter());
			hbaseDO.setData(jsonData);
			
			String tradeOrderId = orderTransferDO.getTransferOrderId();
			
			DumpOrderDetailHbaseDO detailHbaseDO = new DumpOrderDetailHbaseDO();
			detailHbaseDO.setTradeOrderId(tradeOrderId);
			
			detailHbaseDO.setActFlowData(JSON.toJSONString(renderUserActFlowDOList(orderTransferDO.getUserActFlows()), new NumberValueFilter()));
			detailHbaseDO.setCallbackData(JSON.toJSONString(renderCallbackNotifyDOList(orderTransferDO.getOrderCallbacks()), new NumberValueFilter()));
			detailHbaseDO.setDetailData(jsonData);
			detailHbaseDO.setPayFlowData(JSON.toJSONString(renderPayFlowDOList(orderTransferDO.getPayFlows()), new NumberValueFilter()));
			
			orderDetails.add(detailHbaseDO);
			hbaseDOs.add(hbaseDO);
		}
		
		dumpOrderHbaseDAO.batchAddDumpOrder(hbaseDOs);
		dumpOrderHbaseDAO.batchAddOrderDetail(orderDetails);
		
	}
	
	@Override
	public List<OrderWithdrawDumpDO> findUnHandleWithdrawOrder(Integer taskItemNum, List<String> taskItems, Integer fetchNum) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		int dumpStartDayByPlus = ConfigUtil.dumpStartDayByPlus;
		int dumpOrderStartDay = beforeDay + dumpStartDayByPlus;
		Date dumpOrderStartDate = getBeforeDate(new Date(), dumpOrderStartDay);
		String startTime = null;
		
		if (dumpStartDayByPlus >= 0) {
			startTime = DateUtil.formatDate(dumpOrderStartDate, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
		}
		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";

		return orderWithdrawMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum, startTime, endTime);
	}

	@Override
	public boolean updateHandleWithdrawOrder(String tradeOrderNo, List<Integer> oldStatus, Integer newStatus, Integer num) {
		orderWithdrawMysqlDAO.updateHandleForSchedule(tradeOrderNo, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public boolean batchUpdateHandleWithdrawOrder(OrderWithdrawDumpDO[] orderWithdrawDOs, List<Integer> oldStatus, Integer newStatus, Integer num) {
		List<String> orderNos = new ArrayList<String>();
		for (OrderWithdrawDumpDO orderWithdrawDO : orderWithdrawDOs) {
			orderNos.add(orderWithdrawDO.getWithdrawOrderId());
		}
		orderWithdrawMysqlDAO.batchUpdateHandleForSchedule(orderNos, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public void dumpWithdrawOrder(OrderWithdrawDumpDO[] orderWithdrawDOs) {

		List<DumpOrderHbaseDO> hbaseDOs = new ArrayList<DumpOrderHbaseDO>();
		List<DumpOrderDetailHbaseDO> orderDetails = new ArrayList<DumpOrderDetailHbaseDO>();

		for (OrderWithdrawDumpDO orderWithdrawDO : orderWithdrawDOs) {
			
			OrderWithdraw data = renderOrderWithdraw(orderWithdrawDO);
			
			DumpOrderHbaseDO hbaseDO = renderHbaseDO(orderWithdrawDO.getBusinessType(), (int)orderWithdrawDO.getOrderStatus(), data.getWithdrawCardNo(), orderWithdrawDO.getPayerUserId(), orderWithdrawDO.getOrderId(),
					orderWithdrawDO.getWithdrawOrderId(), Constance.DumpRecordType.WITHDRAW.getCode(), orderWithdrawDO.getAddTime(), orderWithdrawDO.getPlatChannelId());
			
			String jsonData = JSON.toJSONString(data, new NumberValueFilter());
			hbaseDO.setData(jsonData);
			
			String tradeOrderId = orderWithdrawDO.getWithdrawOrderId();
			
			DumpOrderDetailHbaseDO detailHbaseDO = new DumpOrderDetailHbaseDO();
			detailHbaseDO.setTradeOrderId(tradeOrderId);
			detailHbaseDO.setActFlowData(JSON.toJSONString(renderUserActFlowDOList(orderWithdrawDO.getUserActFlows()), new NumberValueFilter()));
			detailHbaseDO.setCallbackData(JSON.toJSONString(renderCallbackNotifyDOList(orderWithdrawDO.getOrderCallbacks()), new NumberValueFilter()));
			detailHbaseDO.setDetailData(jsonData);
			detailHbaseDO.setPayFlowData(JSON.toJSONString(renderPayFlowDOList(orderWithdrawDO.getPayFlows()), new NumberValueFilter()));
			
			orderDetails.add(detailHbaseDO);
			hbaseDOs.add(hbaseDO);
		}
		
		dumpOrderHbaseDAO.batchAddDumpOrder(hbaseDOs);
		dumpOrderHbaseDAO.batchAddOrderDetail(orderDetails);

	}
	
	@Override
	public List<OrderRefundDumpDO> findUnHandleRefundOrder(Integer taskItemNum, List<String> taskItems, Integer fetchNum) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		int dumpStartDayByPlus = ConfigUtil.dumpStartDayByPlus;
		int dumpOrderStartDay = beforeDay + dumpStartDayByPlus;
		Date dumpOrderStartDate = getBeforeDate(new Date(), dumpOrderStartDay);
		String startTime = null;
		
		if (dumpStartDayByPlus >= 0) {
			startTime = DateUtil.formatDate(dumpOrderStartDate, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
		}
		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";

		return orderRefundMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum, startTime, endTime);
	}

	@Override
	public boolean updateHandleRefundOrder(String tradeOrderNo, List<Integer> oldStatus, Integer newStatus, Integer num) {
		orderRefundMysqlDAO.updateHandleForSchedule(tradeOrderNo, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public boolean batchUpdateHandleRefundOrder(OrderRefundDumpDO[] orderRefundDOs, List<Integer> oldStatus, Integer newStatus, Integer num) {
		List<String> orderNos = new ArrayList<String>();
		for (OrderRefundDumpDO orderRefundDO : orderRefundDOs) {
			orderNos.add(orderRefundDO.getRefundOrderId());
		}
		orderRefundMysqlDAO.batchUpdateHandleForSchedule(orderNos, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public void dumpRefundOrder(OrderRefundDumpDO[] orderRefundDOs) {
		

		List<DumpOrderHbaseDO> hbaseDOs = new ArrayList<DumpOrderHbaseDO>();
		List<DumpOrderDetailHbaseDO> orderDetails = new ArrayList<DumpOrderDetailHbaseDO>();
		
		for (OrderRefundDumpDO orderRefundDO : orderRefundDOs) {
			DumpOrderHbaseDO hbaseDO = renderHbaseDO(orderRefundDO.getBusinessType(), (int)orderRefundDO.getOrderStatus(), orderRefundDO.getPayeeUserId(), orderRefundDO.getPayerUserId(), orderRefundDO.getOrderId(),
					orderRefundDO.getRefundOrderId(), Constance.DumpRecordType.REFUND.getCode(), orderRefundDO.getAddTime(), orderRefundDO.getPlatChannelId());
			
			OrderRefund data = renderOrderRefund(orderRefundDO);
			hbaseDO.setSrcOrderId(orderRefundDO.getSrcOrderId());
			String jsonData = JSON.toJSONString(data, new NumberValueFilter());
			hbaseDO.setData(jsonData);
			
			String tradeOrderId = orderRefundDO.getRefundOrderId();
			
			DumpOrderDetailHbaseDO detailHbaseDO = new DumpOrderDetailHbaseDO();
			detailHbaseDO.setTradeOrderId(tradeOrderId);
			detailHbaseDO.setActFlowData(JSON.toJSONString(renderUserActFlowDOList(orderRefundDO.getUserActFlows()), new NumberValueFilter()));
			detailHbaseDO.setCallbackData(JSON.toJSONString(renderCallbackNotifyDOList(orderRefundDO.getOrderCallbacks()), new NumberValueFilter()));
			detailHbaseDO.setDetailData(jsonData);
			detailHbaseDO.setPayFlowData(JSON.toJSONString(renderPayFlowDOList(orderRefundDO.getPayFlows()), new NumberValueFilter()));
			
			orderDetails.add(detailHbaseDO);
			hbaseDOs.add(hbaseDO);
		}
		
		dumpOrderHbaseDAO.batchAddDumpOrder(hbaseDOs);
		dumpOrderHbaseDAO.batchAddOrderDetail(orderDetails);

	}

	@Override
	public List<OrderRechargeDumpDO> findUnHandleRechargeOrder(Integer taskItemNum, List<String> taskItems, Integer fetchNum) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		int dumpStartDayByPlus = ConfigUtil.dumpStartDayByPlus;
		int dumpOrderStartDay = beforeDay + dumpStartDayByPlus;
		Date dumpOrderStartDate = getBeforeDate(new Date(), dumpOrderStartDay);
		String startTime = null;
		
		if (dumpStartDayByPlus >= 0) {
			startTime = DateUtil.formatDate(dumpOrderStartDate, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
		}
		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";

		return orderRechargeMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum, startTime, endTime);
	}

	@Override
	public boolean updateHandleRechargeOrder(String tradeOrderNo, List<Integer> oldStatus, Integer newStatus, Integer num) {
		orderRechargeMysqlDAO.updateHandleForSchedule(tradeOrderNo, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public boolean batchUpdateHandleRechargeOrder(OrderRechargeDumpDO[] orderRechargeDOs, List<Integer> oldStatus, Integer newStatus, Integer num) {
		List<String> orderNos = new ArrayList<String>();
		for (OrderRechargeDumpDO orderRechargeDO : orderRechargeDOs) {
			orderNos.add(orderRechargeDO.getRechargeOrderId());
		}
		orderRechargeMysqlDAO.batchUpdateHandleForSchedule(orderNos, oldStatus, newStatus, num);
		return false;
	}

	@Override
	public void dumpRechargeOrder(OrderRechargeDumpDO[] orderRechargeDumpDOs) {

		List<DumpOrderHbaseDO> hbaseDOs = new ArrayList<DumpOrderHbaseDO>();
		List<DumpOrderDetailHbaseDO> orderDetails = new ArrayList<DumpOrderDetailHbaseDO>();
		
		
		for (OrderRechargeDumpDO orderRechargeDumpDO : orderRechargeDumpDOs) {
			DumpOrderHbaseDO hbaseDO = renderHbaseDO(orderRechargeDumpDO.getBusinessType(), (int) orderRechargeDumpDO.getOrderStatus(), orderRechargeDumpDO.getPayeeUserId(),
					orderRechargeDumpDO.getPayerUserId(), orderRechargeDumpDO.getOrderId(), orderRechargeDumpDO.getRechargeOrderId(), Constance.DumpRecordType.CHARGE.getCode(), orderRechargeDumpDO.getAddTime(), orderRechargeDumpDO.getPlatChannelId());
			
			OrderRecharge data = renderOrderRecharge(orderRechargeDumpDO);
			String jsonData = JSON.toJSONString(data, new NumberValueFilter());
			hbaseDO.setData(jsonData);
			
			String tradeOrderId = orderRechargeDumpDO.getRechargeOrderId();

			DumpOrderDetailHbaseDO detailHbaseDO = new DumpOrderDetailHbaseDO();
			detailHbaseDO.setTradeOrderId(tradeOrderId);
			detailHbaseDO.setActFlowData(JSON.toJSONString(renderUserActFlowDOList(orderRechargeDumpDO.getUserActFlows()), new NumberValueFilter()));
			detailHbaseDO.setCallbackData(JSON.toJSONString(renderCallbackNotifyDOList(orderRechargeDumpDO.getOrderCallbacks()), new NumberValueFilter()));
			detailHbaseDO.setDetailData(jsonData);
			detailHbaseDO.setPayFlowData(JSON.toJSONString(renderPayFlowDOList(orderRechargeDumpDO.getPayFlows()), new NumberValueFilter()));
			
			orderDetails.add(detailHbaseDO);
			hbaseDOs.add(hbaseDO);
		}
		
		
		dumpOrderHbaseDAO.batchAddDumpOrder(hbaseDOs);
		dumpOrderHbaseDAO.batchAddOrderDetail(orderDetails);

	}

	private DumpOrderHbaseDO renderHbaseDO(String businessType, Integer orderStatus, String payeeUserId, String payerUserId, String payOrderId, String tradeOrderId, int tradeOrderType, Date createDate, String channelId) {
		DumpOrderHbaseDO hbaseDO = new DumpOrderHbaseDO();
		hbaseDO.setBussinessType(businessType);
		Integer getOrderStatus = getOrderStatus(orderStatus, tradeOrderType);
		hbaseDO.setOrderStatus(getOrderStatus == null ? orderStatus : getOrderStatus);
		hbaseDO.setPayeeAccountId(payeeUserId);
		hbaseDO.setPayerAccountId(payerUserId);
		hbaseDO.setPayOrderId(payOrderId);
		hbaseDO.setTradeOrderId(tradeOrderId);
		hbaseDO.setTradeOrderType(tradeOrderType);
		hbaseDO.setCreateDate(createDate);
		hbaseDO.setChannelId(channelId);
		return hbaseDO;
	}

	private Integer getOrderStatus(Integer orderStatus, int tradeOrderType) {
		if (tradeOrderType == Constance.DumpRecordType.WITHDRAW.getCode()){
			return getWithDrawStatus(orderStatus);
		}
		if (tradeOrderType == Constance.DumpRecordType.B2C.getCode()){
			return orderStatus;
		}
		if (tradeOrderType == Constance.DumpRecordType.TRANSFER.getCode()){
			return getTransferStatus(orderStatus);
		}
		if (tradeOrderType == Constance.DumpRecordType.REFUND.getCode()){
			return getRefundStatus(orderStatus);
		}
		if (tradeOrderType == Constance.DumpRecordType.CHARGE.getCode()){
			return getChargeStatus(orderStatus);
		}
		return null;
	}

	private Integer getChargeStatus(Integer orderStatus) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(0, 1);
		map.put(1, 1);
		map.put(2, 2);
		map.put(3, 3);
		map.put(4, 4);
		return map.get(orderStatus);
	}

	private Integer getRefundStatus(Integer orderStatus) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(0, 1);
		map.put(1, 1);
		map.put(3, 2);
		map.put(2, 3);
		map.put(4, 4);
		return map.get(orderStatus);
	}

	private Integer getTransferStatus(Integer orderStatus) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(0, 1);
		map.put(3, 1);
		map.put(6, 1);
		map.put(2, 2);
		map.put(4, 2);
		map.put(1, 3);
		map.put(7, 3);
		map.put(5, 4);
		return map.get(orderStatus);
	}

	private Integer getWithDrawStatus(Integer orderStatus) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(0, 1);
		map.put(1, 1);
		map.put(5, 1);
		map.put(3, 2);
		map.put(2, 3);
		map.put(6, 3);
		map.put(4, 4);
		map.put(8, 8);
		return map.get(orderStatus);
	}

	@Override
	public void addSuccessDumpRecord(int type) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		String day = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR);
		dumpRecordMysqlDAO.addSuccessDumpRecord(type, day);
	}

	@Override
	public void addFailedDumpRecord(int type) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		String day = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR);
		dumpRecordMysqlDAO.addFailedDumpRecord(type, day);
	}
	
	@Override
	public void addSuccessDumpRecordByCount(int type, int num) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		String day = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR);
		dumpRecordMysqlDAO.addSuccessDumpRecordByCount(type, day, num);
	}
	
	@Override
	public void addFailedDumpRecordByCount(int type, int num) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		String day = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR);
		dumpRecordMysqlDAO.addFailedDumpRecordByCount(type, day, num);
	}

	private OrderB2C renderOrderB2C(OrderB2cDumpDO order) {
		if (order == null)
			return null;
		OrderB2C result = new OrderB2C();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		result.setGuaranteeTypeStr(getB2CGuaranteeTypeStr(order.getGuaranteeType()));
		Constance.TradeOrderType orderType = Constance.TradeOrderType.valueOf(order.getOrderType() == null ? -1 : order.getOrderType());
		result.setOrderTypeStr(orderType == null ? DEFAULTTYPE : orderType.getDesc());
		Constance.B2COrderState b2cOrderState = Constance.B2COrderState.valueOf(order.getOrderStatus() == null ? -1 : order.getOrderStatus());
		result.setOrderStatusStr(b2cOrderState == null ? DEFAULTTYPE : b2cOrderState.getDesc());
		result.setInnerStatusStr(getInnerStatusStr(order.getInnerStatus() == null ? -1 : order.getInnerStatus()));
		result.setChannelTypeStr(getB2COrderChannelStr(order.getOrderChannel()));
		// Constance.PayType payType =
		// Constance.PayType.valueOf(order.getPayType() == null ?
		// -1:order.getPayType());
		// result.setPayTypeStr(payType == null ?
		// getpayTypeByBankId(order.getPayBankId()) : payType.getDesc());
		result.setPayTypeStr(getpayTypeByBankId(order.getPayBankId()));
		result.setOrderChannelStr(getB2COrderChannelStr(order.getOrderChannel()));
		result.setProductTypeStr(getB2CProductTypeStr(order.getProductType()));
		result.setBusinessTypeDes(getBusinessTypeDesc(order.getBusinessType()));
		result.setTradeAmount(CurrencyUtil.fen2yuan(order.getTradeAmount()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		result.setPlatChannelIdDesc(getPlatChannelIdDesc(order.getPlatChannelId()));
		result.setFee(CurrencyUtil.fen2yuan(order.getFee()));
		return result;
	}

	private String getPlatChannelIdDesc(String platChannelId) {
		if (StringUtils.isEmpty(platChannelId)){
			return "未知";
		} else  if (platChannelId.equals("99")) {
			return "缴费";
		}
		return "商城";
	}
	
	private String getB2CGuaranteeTypeStr(Integer guaranteeType) {
		if (guaranteeType == 0)
			return "及时";
		else if (guaranteeType == 1)
			return "担保";
		else
			return DEFAULTTYPE;
	}

	private String getInnerStatusStr(Integer innerStatus) {
		if (innerStatus == 0)
			return "正常";
		else if (innerStatus == 1)
			return "作废";
		else
			return DEFAULTTYPE;
	}

	private String getB2COrderChannelStr(Integer orderChannel) {
		if (orderChannel == 1)
			return "网站";
		else if (orderChannel == 2)
			return "手机";
		else if (orderChannel == 3)
			return "微信";
		else
			return DEFAULTTYPE;
	}

	private String getB2CProductTypeStr(Integer productType) {
		if (productType == 1)
			return "实物";
		else if (productType == 2)
			return "虚拟";
		else
			return DEFAULTTYPE;
	}

	private String getRECRechargeChannelStr(Byte rechargeChannel) {
		if (rechargeChannel == 1)
			return "网站";
		else if (rechargeChannel == 2)
			return "手机";
		else if (rechargeChannel == 3)
			return "微信";
		else if (rechargeChannel == 4)
			return "内部";
		else
			return DEFAULTTYPE;
	}

	private String getWDVerifyTypeStr(Byte verifyType) {
		if (verifyType == 0)
			return "无需审批";
		else if (verifyType == 1)
			return "需要审批";
		else
			return DEFAULTTYPE;
	}

	private String getPayFlowHandleStatusStr(int handleStatus) {
		if (handleStatus == 0)
			return "未处理";
		else if (handleStatus == 1)
			return "已处理";
		else
			return DEFAULTTYPE;
	}

	private String getPayFlowPayerCardActTypeStr(Integer payerActType) {
		if (payerActType == 1)
			return "对私账户";
		else if (payerActType == 2)
			return "对公账户";
		else
			return DEFAULTTYPE;
	}

	private String getpayTypeByBankId(String bankId) {
		if (StringUtil.isBlank(bankId))
			return DEFAULTTYPE;
		if (bankId.startsWith("0"))
			return "余额支付";
		else if (bankId.startsWith("1"))
			return "网银支付";
		else if (bankId.startsWith("2"))
			return "快捷支付";
		else if (bankId.startsWith("3")) {
			if (("3000000").equals(bankId))
				return "支付宝WAP支付";
			else if (("3000001").equals(bankId))
				return "微信公众号支付";
			else if (("3000002").equals(bankId))
				return "微信APP支付";
			else if (("3000003").equals(bankId))
				return "微信APP支付(ios)";
			else if (("3000004").equals(bankId))
				return "支付宝扫码";
			else if (("3000005").equals(bankId))
				return "微信扫码";
			else  if(("3000006").equals(bankId))
				return "微信反扫码";
			else  if(("3000007").equals(bankId))
				return "支付宝反扫码";
			else
				return DEFAULTTYPE;
		} else if (bankId.startsWith("4"))
			return "转帐支付类型";
		else if (bankId.startsWith("5"))
			return "提现";
		else if (bankId.startsWith("6"))
			return "代发到银行";
		else if (bankId.startsWith("7"))
			return "代发到第三方支付账户";
		else
			return DEFAULTTYPE;
	}


	private static class NumberValueFilter implements ValueFilter {

		@Override
		public Object process(Object object, String name, Object value) {
			if (value instanceof BigDecimal) {
				return value.toString();
			}
			return value;
		}
	}


	private Date getBeforeDate(Date date, int beforeDay) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, -beforeDay);

		return c.getTime();
	}

	private OrderRecharge renderOrderRecharge(OrderRechargeDumpDO order) {
		if (order == null)
			return null;
		OrderRecharge result = new OrderRecharge();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.ChargeOrderState rechargeStatus = Constance.ChargeOrderState.valueOf(order.getOrderStatus() == null ? -1 : order.getOrderStatus());
		result.setRechargeStatusStr(rechargeStatus == null ? DEFAULTTYPE : rechargeStatus.getDesc());
		// Constance.PayType payType =
		// Constance.PayType.valueOf(order.getPayType() == null ?
		// -1:order.getPayType());
		// result.setPayTypeStr(payType == null ?
		// getpayTypeByBankId(order.getPayBankId()) : payType.getDesc());
		result.setPayTypeStr(getpayTypeByBankId(order.getPayBankId()));
		result.setRechargeChannelStr(getRECRechargeChannelStr(order.getRechargeChannel() == null ? -1 : order.getRechargeChannel()));
		result.setInnerStatusStr(getInnerStatusStr(order.getInnerStatus() == null ? -1 : order.getInnerStatus()));
		Constance.CardActType bankCardActType = Constance.CardActType.valueOf(order.getBankCardActType() == null ? -1 : order.getBankCardActType());
		result.setBankCardActType(bankCardActType == null ? DEFAULTTYPE : bankCardActType.getDesc());
		Constance.CardType bankCardType = Constance.CardType.valueOf(order.getBankCardType() == null ? -1 : order.getBankCardType());
		result.setBankCardType(bankCardType == null ? DEFAULTTYPE : bankCardType.getDesc());
		result.setBusinessTypeDes(getBusinessTypeDesc(order.getBusinessType()));
		result.setRechargeAmount(CurrencyUtil.fen2yuan(order.getRechargeAmount()));
		result.setRechargeFee(CurrencyUtil.fen2yuan(order.getRechargeFee()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		result.setPlatChannelIdDesc(getPlatChannelIdDesc(order.getPlatChannelId()));
		result.setBankCardNo(YHBFSensitiveEnc.sensitiveDec(order.getBankCardNo(), "cardno"));
		return result;
	}

	private OrderRefund renderOrderRefund(OrderRefundDumpDO order) {
		if (order == null)
			return null;
		OrderRefund result = new OrderRefund();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.RefundType refundType = Constance.RefundType.valueOf(order.getRefundType() == null ? -1 : order.getRefundType());
		result.setRefundTypeStr(refundType == null ? DEFAULTTYPE : refundType.getDesc());
		Constance.RefundOrderState refundStatus = Constance.RefundOrderState.valueOf(order.getOrderStatus() == null ? -1 : order.getOrderStatus());
		result.setRefundStatusStr(refundStatus == null ? DEFAULTTYPE : refundStatus.getDesc());
		result.setRefundChannelStr(getRECRechargeChannelStr(order.getRefundChannel() == null ? -1 : order.getRefundChannel()));
		result.setInnerStatusStr(getInnerStatusStr(order.getInnerStatus() == null ? -1 : order.getInnerStatus()));
		String businessTypeDesc = getBusinessTypeDesc(order.getBusinessType());
		result.setBusinessTypeDes(StringUtil.isBlank(businessTypeDesc) ? "未知" : businessTypeDesc);
		result.setRefundAmount(CurrencyUtil.fen2yuan(order.getRefundAmount()));
		result.setRefundFee(CurrencyUtil.fen2yuan(order.getRefundFee()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		result.setPlatChannelIdDesc(getPlatChannelIdDesc(order.getPlatChannelId()));
		return result;
	}

	private String getBusinessTypeDesc(String businessType){
		RiskTradeTypeDO riskTradeTypeDOById = riskTradeTypeService.getRiskTradeTypeDOById(businessType);
		return riskTradeTypeDOById.getTradeTypeName();
	}
	
	private String getPaychannelDesc(String paychannel){
		if (payChannelList == null) {
			payChannelList = new HashMap<>();
			ChannelRpcService channelRpcService = (ChannelRpcService) dubboClientFactory.getDubboClient("channelRpcService");
			List<ChannelRsp> qryChannelList = channelRpcService.qryChannelList();
			if (!CollectionUtils.isEmpty(qryChannelList)) {
				for (ChannelRsp channelRsp : qryChannelList) {
					payChannelList.put(channelRsp.getChannelId(), channelRsp.getChannelName());
				}
			}
		}
		return payChannelList.get(paychannel);
	}
	
	private OrderTransfer renderOrderTransfer(OrderTransferDumpDO order) {
		if (order == null)
			return null;
		OrderTransfer result = new OrderTransfer();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.TransferType transferType = Constance.TransferType.valueOf(order.getTransferType() == null ? -1 : order.getTransferType());
		result.setTransferTypeStr(transferType == null ? DEFAULTTYPE : transferType.getDesc());
		Constance.TransferOrderState transferStatus = Constance.TransferOrderState.valueOf(order.getOrderStatus() == null ? -1 : order.getOrderStatus());
		result.setTransferStatusStr(transferStatus == null ? DEFAULTTYPE : transferStatus.getDesc());
		result.setTransferChannelStr(getRECRechargeChannelStr(order.getTransferChannel() == null ? -1 : order.getTransferChannel()));
		result.setInnerStatusStr(getInnerStatusStr(order.getInnerStatus() == null ? -1 : order.getInnerStatus()));
		Constance.CardActType cardactType = Constance.CardActType.valueOf(order.getPayeeCardActType() == null ? -1 : order.getPayeeCardActType());
		result.setCardactTypeStr(cardactType == null ? DEFAULTTYPE : cardactType.getDesc());
		Constance.BaseBusinessStatus busniessStatus = Constance.BaseBusinessStatus.valueOf(order.getBusniessStatus());
		result.setBusniessStatusStr(busniessStatus == null ? DEFAULTTYPE : busniessStatus.getDesc());
		Constance.TradeIfUserPwd ifUserPwd = Constance.TradeIfUserPwd.valueOf(order.getIsUsePwd());
		result.setIsUsePwdStr(ifUserPwd == null ? DEFAULTTYPE : ifUserPwd.getDesc());
		Constance.TransferBusinessID busniessStr = Constance.TransferBusinessID.valueOf(order.getBusniessId());
		result.setBusniessId(busniessStr == null ? DEFAULTTYPE : busniessStr.getDesc());
		result.setBusinessTypeDes(getBusinessTypeDesc(order.getBusinessType()));
		result.setTransferAmount(CurrencyUtil.fen2yuan(order.getTransferAmount()));
		result.setTransferFee(CurrencyUtil.fen2yuan(order.getTransferFee()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		result.setPlatChannelIdDesc(getPlatChannelIdDesc(order.getPlatChannelId()));
		result.setCardNo(deCodeMessage(order.getPayeeCardNo()));
		return result;
	}

	private OrderWithdraw renderOrderWithdraw(OrderWithdrawDumpDO order) {
		if (order == null)
			return null;
		OrderWithdraw result = new OrderWithdraw();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.WithDrawType withdrawType = Constance.WithDrawType.valueOf(order.getWithdrawType() == null ? -1 : order.getWithdrawType());
		result.setWithdrawTypeStr(withdrawType == null ? DEFAULTTYPE : withdrawType.getDesc());
		Constance.WithdrawState withdrawStatus = Constance.WithdrawState.valueOf(order.getOrderStatus() == null ? -1 : order.getOrderStatus());
		result.setWithdrawStatusStr(withdrawStatus == null ? DEFAULTTYPE : withdrawStatus.getDesc());
		result.setWithdrawChannelStr(getRECRechargeChannelStr(order.getWithdrawChannel() == null ? -1 : order.getWithdrawChannel()));
		result.setInnerStatusStr(getInnerStatusStr(order.getInnerStatus() == null ? -1 : order.getInnerStatus()));
		result.setVerifyTypeStr(getWDVerifyTypeStr(order.getVerifyType() == null ? -1 : order.getVerifyType()));
		Constance.BaseBusinessStatus busniessStatus = Constance.BaseBusinessStatus.valueOf(order.getBusniessStatus());
		result.setBusniessStatusStr(busniessStatus == null ? DEFAULTTYPE : busniessStatus.getDesc());
		Constance.WithdrawWay withdrawWay = Constance.WithdrawWay.valueOf(order.getWithdrawWay() == null ? -1 : Integer.parseInt(order.getWithdrawWay()));
		result.setWithdrawWay(withdrawWay == null ? DEFAULWITHTTYPE : withdrawWay.getDesc());
		result.setBusinessTypeDes(getBusinessTypeDesc(order.getBusinessType()));
		result.setWithdrawAmount(CurrencyUtil.fen2yuan(order.getWithdrawAmount()));
		result.setWithdrawFee(CurrencyUtil.fen2yuan(order.getWithdrawFee()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		result.setPlatChannelIdDesc(getPlatChannelIdDesc(order.getPlatChannelId()));
		LOG.info("Order WithdrawCardNo is {}", order.getWithdrawCardNo());
		result.setWithdrawCardNo(YHBFSensitiveEnc.sensitiveDec(order.getWithdrawCardNo(), "cardno"));
		return result;
	}

	// 提现的实体里有部分加密字段在此解密
	private String deCodeMessage(String message) {
		if (StringUtil.isNotBlank(message)) {
			try {
				return YHBFUtil.decrypt(message);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return message;
	}
	
	private List<OrderRefund> renderRefundDOList(List<OrderRefundDO> list){
		List<OrderRefund> result = null;
		if(list != null && list.size() > 0){
			result = new ArrayList<OrderRefund>(list.size());
			OrderRefund dump = null;
			for(OrderRefundDO item : list){
				dump = new OrderRefund();
				dump = renderOrderRefund(item);
				result.add(dump);
			}
		}
		return result;
	}
	
	private List<PayFlow> renderPayFlowDOList(List<PayFlowDO> list){
		List<PayFlow> result = null;
		if(list != null && list.size() > 0){
			result = new ArrayList<PayFlow>(list.size());
			PayFlow dump = null;
			for(PayFlowDO item : list){
				dump = new PayFlow();
				dump = renderPayFlow(item);
				result.add(dump);
			}
		}
		return result;
	}
	
	private List<OrderCallback> renderCallbackNotifyDOList(List<CallbackNotifyDO> list){
		List<OrderCallback> result = null;
		if(list != null && list.size() > 0){
			result = new ArrayList<OrderCallback>(list.size());
			OrderCallback dump = null;
			for(CallbackNotifyDO item : list){
				dump = new OrderCallback();
				dump = renderOrderCallback(item);
				result.add(dump);
			}
		}
		return result;
	}
	
	private List<UserActFlow> renderUserActFlowDOList(List<UserActFlowDO> list){
		List<UserActFlow> result = null;
		if(list != null && list.size() > 0){
			result = new ArrayList<UserActFlow>(list.size());
			UserActFlow dump = null;
			for(UserActFlowDO item : list){
				dump = new UserActFlow();
				dump = renderAccountFlow(item);
				result.add(dump);
			}
		}
		return result;
	}
	
	private List<OrderSettle> renderSettleOrderDOList(List<SettleOrderDO> list){
		List<OrderSettle> result = null;
		if(list != null && list.size() > 0){
			result = new ArrayList<OrderSettle>(list.size());
			OrderSettle dump = null;
			for(SettleOrderDO item : list){
				dump = new OrderSettle();
				dump = renderOrderSettle(item);
				result.add(dump);
			}
		}
		return result;
	}
	
	private PayFlow renderPayFlow(PayFlowDO order){
		if(order == null) return null;
		PayFlow result = new PayFlow();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.UserType payerUserType = Constance.UserType.valueOf(order.getPayerUserType() == null ? -1:order.getPayerUserType());
		result.setPayerUserTypeStr(payerUserType == null ? DEFAULTTYPE : payerUserType.getDesc());
		Constance.UserActType payerActType = Constance.UserActType.valueOf(order.getPayerActType() == null ? -1:order.getPayerActType());
		result.setPayerActTypeStr(payerActType == null ? DEFAULTTYPE : payerActType.getDesc());
		Constance.CardType payerCardType = Constance.CardType.valueOf(order.getPayerCardType() == null ? -1:order.getPayerCardType());
		result.setPayerCardTypeStr(payerCardType == null ? DEFAULTTYPE : payerCardType.getDesc());
		result.setPayerCardActTypeStr(getPayFlowPayerCardActTypeStr(order.getPayerCardActType() == null ? -1:order.getPayerCardActType()));
		Constance.IdCardType identityType = Constance.IdCardType.valueOf(order.getPayerIdentityType() == null ? -1:order.getPayerIdentityType());
		result.setPayerIdentityTypeStr(identityType == null ? DEFAULTTYPE : identityType.getDesc());
		Constance.UserType payeeUserType = Constance.UserType.valueOf(order.getPayeeUserType() == null ? -1:order.getPayeeUserType());
		result.setPayeeUserTypeStr(payeeUserType == null ? DEFAULTTYPE : payeeUserType.getDesc());
		Constance.UserActType payeeActType = Constance.UserActType.valueOf(order.getPayeeActType() == null ? -1:order.getPayeeActType());
		result.setPayeeActTypeStr(payeeActType == null ? DEFAULTTYPE : payeeActType.getDesc());
		result.setPayeeCardActTypeStr(getPayFlowPayerCardActTypeStr(order.getPayerActType() == null ? -1:order.getPayerActType()));
		Constance.PayFlowType payType = Constance.PayFlowType.valueOf(order.getPayType() == null ? -1:order.getPayType());
		result.setPayTypeStr(payType == null ? DEFAULTTYPE : payType.getDesc());
		Constance.TradeOrderType orderType = Constance.TradeOrderType.valueOf(order.getTradeOrderType() == null ? -1:order.getTradeOrderType());
		result.setTradeOrderTypeStr(orderType == null ? DEFAULTTYPE : orderType.getDesc());
		Constance.PayFlowState payStatus = Constance.PayFlowState.valueOf(order.getPayStatus() == null ? -1:order.getPayStatus());
		result.setPayStatusStr(payStatus == null ? DEFAULTTYPE : payStatus.getDesc());
		result.setHandleStatusStr(getPayFlowHandleStatusStr(order.getHandleStatus()));
		result.setPayChannelStr(getPaychannelDesc(order.getPayChannelId()));
		result.setAmount(CurrencyUtil.fen2yuan(order.getAmount()));
		
		result.setPayerBankAct(deCodeMessage(order.getPayerBankAct()));
		result.setPayerIdentityNo(deCodeMessage(order.getPayerIdentityNo()));
		result.setPayerMobile(deCodeMessage(order.getPayerMobile()));
		result.setPayeeBankAct(deCodeMessage(order.getPayeeBankAct()));
		result.setPayeeActNo(YHBFSensitiveEnc.sensitiveDec(order.getPayeeActNo(), "cardno"));
		return result;
	}

	private OrderCallback renderOrderCallback(CallbackNotifyDO order){
		if(order ==null) return null;
		OrderCallback result = new OrderCallback();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.NotifyState status = Constance.NotifyState.valueOf(order.getStatus() == null ? -1:order.getStatus());
		result.setStatusStr(status == null ? DEFAULTTYPE : status.getDesc());
		Constance.CallBackOrderState orderStatus = Constance.CallBackOrderState.valueOf(order.getOrderStatus() == null ? -1:order.getOrderStatus());
		result.setOrderStatusStr(orderStatus == null ? DEFAULTTYPE : orderStatus.getDesc());
		Constance.TradeOrderType orderType = Constance.TradeOrderType.valueOf(order.getOrderType() == null ? -1:order.getOrderType());
		result.setOrderTypeStr(orderType == null ? DEFAULTTYPE : orderType.getDesc());
		
		result.setTotalFee(CurrencyUtil.fen2yuan(order.getTotalFee()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		return result;
	}

	private OrderSettle renderOrderSettle(SettleOrderDO order){
		if(order ==null) return null;
		OrderSettle result = new OrderSettle();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.OrderSettleType settleType = Constance.OrderSettleType.valueOf(order.getSettleType());
		result.setSettleTypeStr(settleType == null ? DEFAULTTYPE : settleType.getDesc());
		Constance.OrderSettleState state = Constance.OrderSettleState.valueOf(order.getState());
		result.setStateStr(state == null ? DEFAULTTYPE : state.getDesc());
		
		result.setSettleAmount(CurrencyUtil.fen2yuan(order.getSettleAmount()));
		result.setHandlingFee(CurrencyUtil.fen2yuan(order.getHandlingFee()));
		result.setFundsToAccount(CurrencyUtil.fen2yuan(order.getFundsToAccount()));
		return result;
	}

	private UserActFlow renderAccountFlow(UserActFlowDO order){
		if(order ==null) return null;
		UserActFlow result = new UserActFlow();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.UserActType actType = Constance.UserActType.valueOf(order.getActType() == null ? -1:order.getActType());
		result.setActTypeStr(actType == null ? DEFAULTTYPE : actType.getDesc());
		Constance.TradeOrderType orderType = Constance.TradeOrderType.valueOf(order.getTradeType()== null ? -1:order.getTradeType());
		result.setTradeTypeStr(orderType == null ? DEFAULTTYPE : orderType.getDesc());
		
		result.setActBalance(CurrencyUtil.fen2yuan(order.getActBalance()));
		result.setTranAmount(CurrencyUtil.fen2yuan(order.getTranAmount()));
		return result;
	}
	
	private OrderRefund renderOrderRefund(OrderRefundDO order){
		if(order == null) return null;
		OrderRefund result = new OrderRefund();
		BeanUtils.copyProperties(order, result);
		// 自定义处理
		Constance.RefundType refundType = Constance.RefundType.valueOf(order.getRefundType() == null ? -1:order.getRefundType());
		result.setRefundTypeStr(refundType == null ? DEFAULTTYPE : refundType.getDesc());
		Constance.RefundOrderState refundStatus = Constance.RefundOrderState.valueOf(order.getOrderStatus() == null ? -1:order.getOrderStatus());
		result.setRefundStatusStr(refundStatus == null ? DEFAULTTYPE : refundStatus.getDesc());
		result.setRefundChannelStr(getRECRechargeChannelStr(order.getRefundChannel() == null ? -1:order.getRefundChannel()));
		result.setInnerStatusStr(getInnerStatusStr(order.getInnerStatus() == null ? -1:order.getInnerStatus()));
		result.setBusinessTypeDes(getBusinessTypeDesc(result.getBusinessType()));
		result.setRefundAmount(CurrencyUtil.fen2yuan(order.getRefundAmount()));
		result.setRefundFee(CurrencyUtil.fen2yuan(order.getRefundFee()));
		result.setPayAmount(CurrencyUtil.fen2yuan(order.getPayAmount()));
		return result;
	}

	@Override
	public void updateDumpRecordAddTime(int type) {
		int beforeDay = ConfigUtil.dumpBeforeDay;
		Date date = getBeforeDate(new Date(), beforeDay);
		String day = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR);
		dumpRecordMysqlDAO.updateDumpRecordAddTime(type, day);
	}
}
